package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;

public class PitchGameOn extends State implements Constants {

	private static PitchGameOn instance = null;	

	public static PitchGameOn state(){
		if(instance == null){
			instance = new PitchGameOn();
			instance.name = "Game On";
		}
		return instance;
	}

	@Override
	public void enter(BaseEntity owner) {
		Pitch pitch = (Pitch) owner;
		pitch.ballInPlay = true;
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
	}

	@Override
	public void exit(BaseEntity owner) {
		Pitch pitch = (Pitch) owner;
		pitch.ballInPlay = false;
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		Pitch pitch = (Pitch) owner;
		switch(tgram.msg){
		case TEAMS_PREPARE_FOR_KICK_OFF:
			pitch.FSM().changeState(PitchPrepareForKickOff.state());
			return true;
		case GOAL_SCORED:
			int teamToScore = (Integer) tgram.extraInfo[0];
			pitch.teamScore[teamToScore]++;
			pitch.FSM().changeState(PitchPrepareForKickOff.state());
		}
		return false;
	}

}
